﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CompanyBussiness
{
    public class ProductManagement:IProductManagement
    {
        private CompanyDataContext companydata = new CompanyDataContext();

        public virtual List<TypeProduct> GetParentTypeProduct(int languagesID, Boolean typeProductStatus)
        {
            var typeProduct = (from tp in companydata.TypeProducts
                            where tp.LanguagesID == languagesID && tp.ParentTypeProductID == null && tp.TypeProductStatus == typeProductStatus
                            select tp).ToList();
            return typeProduct;
        }

        public virtual List<TypeProduct> GetParentTypeProductAll(int languagesID)
        {
            var typeProduct = (from tp in companydata.TypeProducts
                               where tp.LanguagesID == languagesID && tp.ParentTypeProductID == null
                               select tp).ToList();
            return typeProduct;
        }

        public virtual List<TypeProduct> GetChildrenTypeProductAll(int languagesID, Boolean typeProductStatus)
        {
            var typeProduct = (from tp in companydata.TypeProducts
                               where tp.LanguagesID == languagesID && tp.ParentTypeProductID == null && tp.TypeProductStatus==typeProductStatus
                               select tp).ToList();
            return typeProduct;
        }

        public virtual List<TypeProduct> GetChildrenTypeProductByParentID(int parentTypeProductID, int languagesID, Boolean typeProductStatus, int take, int StartRow)
        {
            var typeProduct = (from tp in companydata.TypeProducts
                               where tp.LanguagesID == languagesID && tp.ParentTypeProductID == parentTypeProductID && tp.TypeProductStatus == typeProductStatus
                               select tp);
            if(typeProduct.Count()<take)
                return typeProduct.ToList();
            else
                return typeProduct.Skip(StartRow).Take(take).ToList();
        }

        public virtual List<TypeProduct> GetChildrenTypeProductByParentID(int parentTypeProductID, int languagesID, Boolean typeProductStatus)
        {
            var typeProduct = (from tp in companydata.TypeProducts
                               where tp.LanguagesID == languagesID && tp.ParentTypeProductID == parentTypeProductID && tp.TypeProductStatus == typeProductStatus
                               select tp);

                return typeProduct.ToList();
            
        }

        public virtual List<TypeProduct> GetChildrenTypeProductByParentIDAll(int parentTypeProductID, int languagesID)
        {
            var typeProduct = (from tp in companydata.TypeProducts
                               where tp.LanguagesID == languagesID && tp.ParentTypeProductID == parentTypeProductID
                               select tp);
            return typeProduct.ToList();
        }

        public virtual TypeProduct GetTypeProductByID(int typeProductID)
        {
            if (typeProductID != 0)
            {
                var typeProduct = (from tp in companydata.TypeProducts
                                   where tp.TypeProductID == typeProductID
                                   select tp).Single();
                return typeProduct;
            }
            return null;
        }

        public virtual TypeProduct GetTypeProductByNameSingle(string typeProductName)
        {
            var typeProduct = (from tp in companydata.TypeProducts
                               where tp.TypeProductName.Contains(typeProductName)==true
                               select tp).Single();
            return typeProduct;
        }

        public virtual List<TypeProduct> GetTypeProductByName(string typeProductName, int languagesID, Boolean typeProductStatus)
        {
            var typeProduct = (from tp in companydata.TypeProducts
                               where tp.TypeProductName.Contains(typeProductName) == true && tp.LanguagesID == languagesID && tp.TypeProductStatus == typeProductStatus
                              select tp).ToList();
            return typeProduct;
        }
        public virtual List<TypeProduct> GetTypeProductByStatus(int languagesID, Boolean typeProductStatus)
        {
            var typeProduct = (from tp in companydata.TypeProducts
                               where tp.TypeProductStatus == typeProductStatus && tp.LanguagesID == languagesID
                               select tp).ToList();
            return typeProduct;
        }

        public virtual int GetTopTypeProductID(int languagesID)
        {
            var type = (from tp in companydata.TypeProducts
                            where tp.ParentTypeProductID == null && tp.LanguagesID == languagesID
                            orderby tp.TypeProductID ascending
                            select tp).FirstOrDefault();

            return type != null ? type.TypeProductID : 0;
        }

        public virtual List<Product> GetProductByTypeProduct(int typeProductID, int take, int StartRow)
        {
            var product = (from pr in companydata.Products
                           where pr.TypeProductID == typeProductID
                          select pr);
            if (product.Count() < take)
                return product.ToList();
            else
                return product.Skip(StartRow).Take(take).ToList();
        }

        public virtual List<Product> GetProductByTypeProductIDAll(int typeProductID)
        {
            var product = (from pr in companydata.Products
                           where pr.TypeProductID == typeProductID 
                           select pr); 
            return product.ToList();
        }

        public virtual List<Product> GetProductSearch(Product product, DateTime DateForm, DateTime DateTo, int take, int StartRow)
        {
            var pro = (from p in companydata.Products
                       join tp in companydata.TypeProducts on p.TypeProductID equals tp.TypeProductID
                       where p.TypeProductID==product.TypeProductID || tp.ParentTypeProductID==product.TypeProductID &&(p.ProductDateIn>=DateForm) && (p.ProductDateIn<=DateTo)
                       orderby p.ProductID descending
                         select p);
            if (product.ProductCode != String.Empty)
            {
                pro = (from p in pro
                         where p.ProductCode == product.ProductCode
                         select p);
            }

            if (product.ProductName != String.Empty)
            {
                pro = (from p in pro
                         where p.ProductName.Contains(product.ProductName)==true
                         select p);
            }

            if (product.ProductStatus != null)
            {
                pro = (from p in pro
                         where p.ProductStatus == product.ProductStatus 
                         select p);
            }

            if (pro.Count() < take)
                return pro.ToList();
            else
                return pro.Skip(StartRow).Take(take).ToList();
        }

        public virtual List<Product> GetProductSearchNotDate(Product product, int take, int StartRow)
        {
            var pro = (from p in companydata.Products
                       join tp in companydata.TypeProducts on p.TypeProductID equals tp.TypeProductID
                       where p.TypeProductID == product.TypeProductID || tp.ParentTypeProductID == product.TypeProductID //&& (p.ProductDateIn >= DateForm) && (p.ProductDateIn <= DateTo)
                       orderby p.ProductID descending
                       select p);
            if (product.ProductCode != String.Empty)
            {
                pro = (from p in pro
                       where p.ProductCode == product.ProductCode
                       select p);
            }

            if (product.ProductName != String.Empty)
            {
                pro = (from p in pro
                       where p.ProductName.Contains(product.ProductName) == true
                       select p);
            }

            if (product.ProductStatus != null)
            {
                pro = (from p in pro
                       where p.ProductStatus == product.ProductStatus
                       select p);
            }

            if (pro.Count() < take)
                return pro.ToList();
            else
                return pro.Skip(StartRow).Take(take).ToList();
        }

        public virtual int GetNumberProductSearchNotDate(Product product)
        {
            var pro = (from p in companydata.Products
                       join tp in companydata.TypeProducts on p.TypeProductID equals tp.TypeProductID
                       where p.TypeProductID == product.TypeProductID || tp.ParentTypeProductID == product.TypeProductID //&& (p.ProductDateIn >= DateForm) && (p.ProductDateIn <= DateTo)
                       orderby p.ProductID descending
                       select p);
            if (product.ProductCode != String.Empty)
            {
                pro = (from p in pro
                       where p.ProductCode == product.ProductCode
                       select p);
            }

            if (product.ProductName != String.Empty)
            {
                pro = (from p in pro
                       where p.ProductName.Contains(product.ProductName) == true
                       select p);
            }

            if (product.ProductStatus != null)
            {
                pro = (from p in pro
                       where p.ProductStatus == product.ProductStatus
                       select p);
            }

            return pro.Count();
        }


        public virtual int GetNumberByTypeProduct(int typeProductID)
        {
            var num = (from ty in companydata.TypeProducts
                       where ty.TypeProductID == typeProductID
                       select ty).Count();
            return num;
        }

        public virtual int GetNumberProductSearch(Product product, DateTime DateForm, DateTime DateTo)
        { 
            var pro = (from p in companydata.Products
                       join tp in companydata.TypeProducts on p.TypeProductID equals tp.TypeProductID
                       where p.TypeProductID == product.TypeProductID || tp.ParentTypeProductID == product.TypeProductID && (p.ProductDateIn >= DateForm) && (p.ProductDateIn <= DateTo)
                       orderby p.ProductID descending
                         select p);
            if (product.ProductCode != String.Empty)
            {
                pro = (from p in pro
                         where p.ProductCode == product.ProductCode
                         select p);
            }

            if (product.ProductName != String.Empty)
            {
                pro = (from p in pro
                         where p.ProductName.Contains(product.ProductName)==true
                         select p);
            }

            if (product.ProductStatus != null)
            {
                pro = (from p in pro
                         where p.ProductStatus == product.ProductStatus 
                         select p);
            }

            return pro.Count();
        }

        public virtual Product GetProductByID(int productID)
        {
            var product = (from pr in companydata.Products
                           where pr.ProductID == productID
                           select pr).Single();
            return product;
        }
        public virtual List<Product> GetProductByName(string productName, int take, int StartRow)
        {
            var product = (from pr in companydata.Products
                           where pr.ProductName.Contains(productName) == true
                           select pr); //co phan trang moi lan lay 10 product (tim kiem product theo tieu de)
            if (product.Count() < take)
                return product.ToList();
            else
                return product.Skip(StartRow).Take(take).ToList();
        }
        public virtual List<Product> GetProductByStatus(int languagesID, Boolean productStatus, int StartRow)
        {
            var product = (from pr in companydata.Products
                           join tp in companydata.TypeProducts on pr.TypeProductID equals tp.TypeProductID
                           where pr.ProductStatus == productStatus && tp.LanguagesID == languagesID
                           select pr); //co phan trang moi lan lay 10 product (tim kiem product theo trang thai)
            if (product.Count() < 10)
                return product.ToList();
            else
                return product.Skip(StartRow).Take(10).ToList();
        }


        public virtual int CreateTypeProduct(int parentTypeProductID, string typeProductName, string typeProductIntro, string typeProductDes, string typeProductImg, Boolean typeProductStatus, Boolean imagesStatus, int languagesID)
        {
            TypeProduct typeProduct = new TypeProduct();
            if (parentTypeProductID == 0)
            {
                typeProduct.ParentTypeProductID = null;
            }
            else
            {
                typeProduct.ParentTypeProductID = parentTypeProductID;
            }

            //typeProduct.ParentTypeProductID = parentTypeProductID;
            typeProduct.TypeProductName = typeProductName;
            typeProduct.TypeProductIntro = typeProductIntro;
            typeProduct.TypeProductDes = typeProductDes;
            typeProduct.TypeProductImg = typeProductImg;
            typeProduct.TypeProductStatus = typeProductStatus;
            typeProduct.LanguagesID = languagesID;
            typeProduct.ImagesStatus = imagesStatus;

            companydata.TypeProducts.InsertOnSubmit(typeProduct);
            companydata.SubmitChanges();

            return typeProduct.TypeProductID;
        }

        public virtual void UpdateTypeProduct(int typeProductID, int parentTypeProductID, string typeProductName, string typeProductIntro, string typeProductDes, string typeProductImg, Boolean typeProductStatus, Boolean imagesStatus, int languagesID)
        {
            var typeProduct = (from tp in companydata.TypeProducts
                               where tp.TypeProductID == typeProductID
                               select tp).Single();
            if (parentTypeProductID == 0)
            {
                typeProduct.ParentTypeProductID = null;
            }
            else
            {
                typeProduct.ParentTypeProductID = parentTypeProductID;
            }

            //typeProduct.ParentTypeProductID = parentTypeProductID;
            typeProduct.TypeProductName = typeProductName;
            typeProduct.TypeProductIntro = typeProductIntro;
            typeProduct.TypeProductDes = typeProductDes;
            typeProduct.TypeProductImg = typeProductImg;
            typeProduct.TypeProductStatus = typeProductStatus;
            typeProduct.LanguagesID = languagesID;
            typeProduct.ImagesStatus = imagesStatus;

            companydata.SubmitChanges();
        }

        public virtual void DeleteTypeProduct(int typeProductID)
        {
            var typeProduct = (from tp in companydata.TypeProducts
                               where tp.TypeProductID == typeProductID
                               select tp).Single();

            companydata.TypeProducts.DeleteOnSubmit(typeProduct);
            companydata.SubmitChanges();
        }


        public virtual int CreateProduct(string productName, string productDes, string productAuthor, string productSkin, int productQuantity, float productPrice, DateTime productDateIn, string productImg, Boolean productStatus, Boolean imagesStatus, int typeProductID, string file, string features)
        {
            Product product = new Product();
            product.ProductName = productName;
            product.ProductDes = productDes;
            product.ProductAuthor = productAuthor;
            product.ProductSkin = productSkin;
            product.ProductQuantity = productQuantity;
            product.ProductPrice = productPrice;
            product.ProductDateIn = productDateIn;
            product.ProductImg = productImg;
            product.ProductStatus = productStatus;
            product.TypeProductID = typeProductID;
            product.ImagesStatus = imagesStatus;
            product.FileAttach = file;
            product.Features = features;

            companydata.Products.InsertOnSubmit(product);
            companydata.SubmitChanges();

            return product.ProductID;
        }

        public virtual void UpdateProduct(int productID, string productName, string productDes, string productAuthor, string productSkin, int productQuantity, float productPrice, DateTime productDateIn, string productImg, Boolean productStatus, Boolean imagesStatus, int typeProductID, string file, string features)
        {
            var product = (from pr in companydata.Products
                           where pr.ProductID == productID
                           select pr).Single();

            product.ProductName = productName;
            product.ProductDes = productDes;
            product.ProductAuthor = productAuthor;
            product.ProductSkin = productSkin;
            product.ProductQuantity = productQuantity;
            product.ProductPrice = productPrice;
            product.ProductDateIn = productDateIn;
            product.ProductImg = productImg;
            product.ProductStatus = productStatus;
            product.TypeProductID = typeProductID;
            product.ImagesStatus = imagesStatus;
            product.FileAttach = file;
            product.Features = features;

            companydata.SubmitChanges();
        }

        public virtual void DeleteProduct(int productID)
        {
            var product = (from pr in companydata.Products
                           where pr.ProductID == productID
                           select pr).Single();

            companydata.Products.DeleteOnSubmit(product);
            companydata.SubmitChanges();
        }

        public virtual string GetNametypeByID(int id)
        {
            var type = (from p in companydata.TypeProducts
                        where p.TypeProductID == id
                        select p).FirstOrDefault();
            if (type == null)
                return "";
            else
                return type.TypeProductName;
        }

        public virtual string GetNameProductByID(int id)
        {
            var product = (from p in companydata.Products
                        where p.ProductID == id
                           select p).FirstOrDefault();
            if (product == null)
                return "";
            else
                return product.ProductName;
        }



        public virtual List<TypeProperty> GetListTypePropertyByProductID(int ProductID)
        {
            var type = (from p in companydata.TypeProperties
                        where p.ProductID == ProductID
                        select p);

            return type.ToList();
        }

        public virtual TypeProperty GetTypePropertyById(int Id)
        {
            var type = (from p in companydata.TypeProperties
                        where p.Id == Id
                        select p).Single();
            return type;
        }


        public virtual int CreateTypeProperty(TypeProperty typeProperty)
        {
           
            companydata.TypeProperties.InsertOnSubmit(typeProperty);
            companydata.SubmitChanges();

            return typeProperty.Id;
        }

        public virtual void UpdateTypeProperty(TypeProperty typeProperty)
        {
            var type = (from p in companydata.TypeProperties
                        where p.Id == typeProperty.Id
                        select p).Single();

            type.TypePropertiesName = typeProperty.TypePropertiesName;
            type.TypePropertiesDes = typeProperty.TypePropertiesDes;
            typeProperty.ProductID = typeProperty.ProductID;

            companydata.SubmitChanges();
            
        }
        public virtual void DeleteTypeProperty(int TypePropertyID)
        {
            var type = (from p in companydata.TypeProperties
                        where p.Id == TypePropertyID
                        select p).Single();

            companydata.TypeProperties.DeleteOnSubmit(type);
            companydata.SubmitChanges();
        }

        public virtual Property GetPropertyByID(int PropertyID)
        {
            var property = (from p in companydata.Properties
                            where p.PropertiesID == PropertyID
                            select p).Single();
            return property;
        }

        public virtual List<Property> GetPropertyByTypeID(int TypePropertyID)
        {
            var property = (from p in companydata.Properties
                            where p.TypeProperties == TypePropertyID
                            select p);
            return property.ToList();
        }

        public virtual int CreateProperty(Property property)
        {
            Property proper = new Property();
            proper.PropertiesName = property.PropertiesName;
            proper.PropertiesDes = property.PropertiesDes;
            proper.TypeProperties = property.TypeProperties;

            companydata.Properties.InsertOnSubmit(property);
            companydata.SubmitChanges();

            return property.PropertiesID;
        }

        public virtual void UpdateProperty(int id, string Name, string Des, int TypeId)
        {
            var proper = (from p in companydata.Properties
                          where p.PropertiesID == id
                        select p).Single();

            proper.PropertiesName =Name;
            proper.PropertiesDes = Des;
            proper.TypeProperties = TypeId;

            companydata.SubmitChanges();
        }

        public virtual void DeleteProperty(int PropertyID)
        {
            var proper = (from p in companydata.Properties
                          where p.PropertiesID == PropertyID
                          select p).Single();

            companydata.Properties.DeleteOnSubmit(proper);
            companydata.SubmitChanges();
        }

    }
}
